package com.znsd.zl.app.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.znsd.zl.app.ocr.utils.Base64Util;
import com.znsd.zl.app.user.annotation.Login;
import com.znsd.zl.app.user.entity.AppUserEntity;
import com.znsd.zl.app.user.service.IAppUserService;
import com.znsd.zl.app.user.utils.RedisUtils;
import com.znsd.zl.app.user.vo.req.AppUserRealAuthReqVO;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.GenerateQueryWrapper;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *     用户系统后台控制器
 * </p>
 * @ClassName AppUserController
 * @Description 客户端用户控制器
 * @author jaypdd
 * @email jaypdd@163.com
 * @date 2020-08-21 17:26:01
 * @Version v1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController<IAppUserService, AppUserEntity, Integer> {

	@Resource
	private RedisUtils redisUtils;

	/**
	 * 分页条件查询
	 * @param appUserEntity  根据字段值进行分页条件查询
	 * @param pageNum        当前请求页码
	 * @param pageSize       每页请求记录数数量
	 * @return 返回分页后的短信日志列表
	 */
	@GetMapping("/pageFind")
	public R pageFind(AppUserEntity appUserEntity,
	                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
	                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
	                  HttpServletRequest req) {
		PageUtils pageUtils = null;
		try{
			// 生成QueryWrapper条件构造器
			QueryWrapper<AppUserEntity> queryWrapper = GenerateQueryWrapper
					.initQueryWrapper(appUserEntity, req.getParameterMap());
			pageUtils = new PageUtils(service.page(new Page<AppUserEntity>(pageNum, pageSize), queryWrapper));
		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		return R.ok().put("page", pageUtils);
	}

	/**
	 * 根据openId完善用户信息
	 * @param user  用户信息
	 * @return      成功 code:200 失败500
	 */
	@PutMapping("/updateInfo")
	public R updateUserInfo(@RequestBody AppUserEntity user, HttpServletRequest request) {
		String token = request.getHeader("token");
		boolean status = service.updateInfo(user, token);
		return status ? R.ok() : R.error("用户信息更新失败");
	}

}
